<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/12/26 11:48
 * Desc: 多套餐
 */
class Service_Api_Spot_Booking_Multiple
{
    private $spot;

    public function __construct($spot)
    {
        $this->spot = $spot;
    }

    public function get_price_calendar($params, $ticket_info = array())
    {
        $spot_id = $this->spot['id'];

        $tickets = Model_Spot_Ticket::get_on_sale_ticket(array('spot_id' => $spot_id));

        $data = array();
        foreach ($tickets as $ticket) {
            // 处理提前预订时间
            $before_time = Model_Spot_Ticket::get_before_time($ticket);

            if ($before_time && strtotime($params['start_date']) < $before_time) {
                $params['start_date'] = date("Y-m-d H:i:s", $before_time);
            }

            $prices = Model_Spot_Ticket_Price::get_price_calendar($spot_id, array('ticket_id' => $ticket['id']), $params);
            foreach ($prices as $key => $price) {
                if (isset($data[$key])) {
                    $stock = max($price['stock'], $data[$key]['stock']);
                    // 检查最小价格
                    if ($price['price'] < $data[$key]['price']) {
                        $data[$key] = $price;
                    }
                    $data[$key]['stock'] = $stock;
                } else {
                    $data[$key] = $price;
                }
            }
        }

        return $data;
    }

    /**
     * 多套餐预订获取套餐金额信息
     * @param $params array start_date:Y-m-d end_date:Y-m-d
     * @param $ticket_info array 页面默认的门票
     * @return array $data = array(
     * 'default' => array(), // 默认选择的报价规格
     * 'sku_list' => array(),
     * 'prices' => array() // 价格列表：多规格或多套餐需要返回
     * );
     */
    public function get_book_suit($params, $ticket_info)
    {
        $spot_id           = $this->spot['id'];
        $date              = Arr::get($params, 'date');
        $default_ticket_id = Arr::get($ticket_info, 'id', '');
        $member_card_id    = Arr::get($params, 'member_card_id', '');
        $seckill_id        = Arr::get($params, 'is_seckill', '');
        $data              = array(
            'prices' => array() // 价格列表：多规格或多套餐需要返回
        );

        $ticket_id = '';
        if($member_card_id){
            $card_product_id  = DB::select()->from('card_member_product')
                ->where('member_card_id', '=', $member_card_id)
                ->and_where('type_id', '=', Model_Spot::$typeid)
                ->and_where('product_id', '=', $spot_id)
                ->execute()->get('id');
            if($card_product_id){
                $card_ticket_id = DB::select()->from('card_member_product_suit')
                    ->where('card_member_product_id', '=', $card_product_id)
                    ->execute()->as_array();
                if(!empty($card_ticket_id)){
                    $ticket_id = array_column($card_ticket_id, 'suit_id');
                }
            }
        }

        if($seckill_id){
            $seckill = DB::select()->from('seckill')
                ->where('id', '=', $seckill_id)
                ->execute()->current();
            $ticket_id = $seckill['suit_id'];
        }

        $tickets = Model_Spot_Ticket::get_on_sale_ticket(array('spot_id' => $spot_id, 'key' => 'id'));
        if (!empty($tickets)) {
            if($ticket_id){
                $tickets_ids = $ticket_id;
            }else{
                $tickets_ids = Arr::pluck($tickets, 'id');
            }
            $skus        = Model_Spot_Ticket_Sku::get_price_sku($spot_id, array('ticket_id' => $tickets_ids));
            if (!empty($skus)) {
                $prices        = array();
                $stock_service = new Service_Spot_Ticket_Stock();
                foreach ($skus as $key => $sku) {
                    $ticket_id = $sku['ticket_id'];
                    $ticket    = $tickets[$ticket_id];

                    // 处理提前预订时间
                    $before_time = Model_Spot_Ticket::get_before_time($ticket);
                    if ($before_time && strtotime($date) < $before_time) {
                       continue;
                    }

                    // 查询报价
                    $price_params            = $sku;
                    $price_params['date']    = $date;
                    $price_params['is_open'] = 1;
                    $price                   = Model_Spot_Ticket_Price::get_price($price_params);
                    if (empty($price)) {
                        unset($skus[$key]);
                        continue;
                    }

                    // 价格处理
                    $stock = $price['number'];
                    if (empty($stock)) {
                        unset($skus[$key]);
                        continue;
                    }
                    $item  = array(
                        'name' => $ticket['title'],
                        'price' => floatval(Currency_Tool::price($price['adultprice'])),
                        'stock' => $stock,
                        'max' => $stock_service->get_stock_limit($ticket, $stock, $date, $params),
                        'ticket_id' => $ticket_id,
                        'sku_id' => $sku['sku_id'],
                        'ticket_info' => $ticket
                    );

                    // 将默认的门票放在最前面
                    if ($default_ticket_id == $ticket_id) {
                        // 插入数组前
                        array_unshift($prices, $item);
                    } else {
                        // 插入数据后面
                        $prices[] = $item;
                    }
                }
                $data['prices'] = $prices;
            }
        }

        return $data;
    }
}